﻿namespace com.vs.Inventory.BL
{
    using com.vs.DataAccess;
    using com.vs.DataAccess.DO;
    using com.vs.General;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;

    public class InventoryHelper
    {
        private static com.vs.Inventory.BL.InventoryHelper _helper = null;

        public string AdjustInventory(Session sess, InventoryChangeDS invChgDS)
        {
            DataHelper helper = null;
            string cmdText = null;
            string str2;
            try
            {
                helper = new DataHelper(sess);
                cmdText = this.buildSQLFromInvChgDS(sess, invChgDS);
                helper.Update(CommandType.Text, cmdText, new IDataParameter[0]);
                str2 = null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                str2 = MessageManager.MSG_GEN_000;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return str2;
        }

        public string AdjustInventory(Session sess, List<InventoryChangeDS> arInvChgDS)
        {
            StringBuilder builder = null;
            DataHelper helper = null;
            string str;
            try
            {
                builder = new StringBuilder();
                foreach (InventoryChangeDS eds in arInvChgDS)
                {
                    builder.Append(this.buildSQLFromInvChgDS(sess, eds));
                }
                helper = new DataHelper(sess);
                helper.StartTransaction();
                helper.Update(CommandType.Text, builder.ToString(), new IDataParameter[0]);
                helper.Commit();
                str = null;
            }
            catch (Exception exception)
            {
                helper.Rollback();
                ExceptionManager.LogException(sess, exception);
                str = MessageManager.MSG_GEN_000 + exception.Message;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return str;
        }

        public string AdjustInventory(Session sess, InventoryDS invDS, string op_OnHand, string op_InTransit, string op_Allocated)
        {
            InventoryChangeDS invChgDS = null;
            string str = null;
            string str2;
            int num = 0;
            try
            {
                num = new AttributeManager().GetAttributeNum(sess, invDS.ITEM, invDS.COMPANY, invDS.ATTRIBUTE1, invDS.ATTRIBUTE2, invDS.ATTRIBUTE3, invDS.ATTRIBUTE4, invDS.ATTRIBUTE5, invDS.ATTRIBUTE6, invDS.ATTRIBUTE7, invDS.ATTRIBUTE8);
                if (((num > 0) && (invDS.ATTRIBUTE_NUM > 0)) && (num != invDS.ATTRIBUTE_NUM))
                {
                    return MessageManager.MSG_INV_009;
                }
                invDS.ATTRIBUTE_NUM = num;
                str = this.ValidateAdjustment(sess, invDS, op_OnHand, op_InTransit, op_Allocated);
                if (!StringManager.IsEmpty(str))
                {
                    return str;
                }
                invChgDS = new InventoryChangeDS {
                    WAREHOUSE = invDS.WAREHOUSE,
                    LOCATION = invDS.LOCATION,
                    LPN = invDS.LPN,
                    PARENT_LPN = invDS.PARENT_LPN,
                    ITEM = invDS.ITEM,
                    COMPANY = invDS.COMPANY,
                    ATTRIBUTE_NUM = invDS.ATTRIBUTE_NUM,
                    INVENTORY_STS = invDS.INVENTORY_STS,
                    QUANTITY = invDS.QUANTITY,
                    QUANTITY_UM = invDS.QUANTITY_UM,
                    OP_ONHANDQTY = op_OnHand,
                    OP_INTRANQTY = op_InTransit,
                    OP_ALLOCQTY = op_Allocated
                };
                str2 = this.AdjustInventory(sess, invChgDS);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str2;
        }

        private string buildSQLFromInvChgDS(Session sess, InventoryChangeDS invChgDS)
        {
            try
            {
                return new StringBuilder().Append("EXEC INV_Adjust ").Append(StringManager.ToSafeSQLString(invChgDS.WAREHOUSE)).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.LOCATION)).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.LPN)).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.PARENT_LPN)).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.ITEM)).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.COMPANY)).Append(", ").Append(invChgDS.ATTRIBUTE_NUM).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.INVENTORY_STS)).Append(", ").Append(invChgDS.QUANTITY).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.QUANTITY_UM)).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.OP_ONHANDQTY)).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.OP_INTRANQTY)).Append(", ").Append(StringManager.ToSafeSQLString(invChgDS.OP_ALLOCQTY)).Append(";").ToString();
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return MessageManager.MSG_GEN_000;
            }
        }

        public string CreateTransferTask(Session sess, InventoryTransDS itDS)
        {
            return null;
        }

        public static com.vs.Inventory.BL.InventoryHelper GetInstance()
        {
            if (_helper == null)
            {
                _helper = new com.vs.Inventory.BL.InventoryHelper();
            }
            return _helper;
        }

        public int GetLocAvailableCapacity(Session sess, string stWarehouse, string stCompany, string stItem, string stLoc, string stQtyUM)
        {
            DataHelper helper = null;
            DataTable table = null;
            string cmdText = null;
            int @int;
            try
            {
                helper = new DataHelper(sess);
                cmdText = "INV_RLocAvailableCapacity";
                table = helper.GetTable(CommandType.StoredProcedure, cmdText, new IDataParameter[] { DataHelper.BuildParameter(sess, "@WAREHOUSE", stWarehouse), DataHelper.BuildParameter(sess, "@COMPANY", stCompany), DataHelper.BuildParameter(sess, "@ITEM", stItem), DataHelper.BuildParameter(sess, "@LOCATION", stLoc), DataHelper.BuildParameter(sess, "@QUANTITY_UM", stQtyUM) });
                if (DataManager.IsEmpty(table))
                {
                    HistoryManager.SaveProcessHistory(sess, HistoryManager.ProcessType.iItem, HistoryManager.Condition.iFailed, MessageManager.MSG_ITM_006, stItem, stCompany, stLoc, null);
                    return 0;
                }
                @int = DataManager.GetInt(table.Rows[0], 0);
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                @int = 0;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return @int;
        }

        public int GetLocCapacity(Session sess, string stWarehouse, string stCompany, string stItem, string stLoc)
        {
            DataHelper helper = null;
            DataTable table = null;
            string cmdText = null;
            int @int;
            int size = 0;
            try
            {
                helper = new DataHelper(sess);
                cmdText = "INV_RItemLocCapacity";
                table = helper.GetTable(CommandType.StoredProcedure, cmdText, new IDataParameter[] { DataHelper.BuildParameter(sess, "@WAREHOUSE", stWarehouse), DataHelper.BuildParameter(sess, "@COMPANY", stCompany), DataHelper.BuildParameter(sess, "@ITEM", stItem), DataHelper.BuildParameter(sess, "@LOCATION", stLoc), DataHelper.BuildOutputParameter(sess, "@Result", DbType.Int32, size) });
                if (DataManager.IsEmpty(table))
                {
                    return 0;
                }
                @int = DataManager.GetInt(table.Rows[0], 0);
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                @int = 0;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return @int;
        }

        public int GetLocCapacity(Session sess, string stWarehouse, string stCompany, string stItem, string stLoc, string stQtyUM)
        {
            DataHelper helper = null;
            DataTable table = null;
            string cmdText = null;
            int num2;
            int size = 0;
            try
            {
                helper = new DataHelper(sess);
                cmdText = "INV_RItemLocCapacityByUM";
                table = helper.GetTable(CommandType.StoredProcedure, cmdText, new IDataParameter[] { DataHelper.BuildParameter(sess, "@WAREHOUSE", stWarehouse), DataHelper.BuildParameter(sess, "@COMPANY", stCompany), DataHelper.BuildParameter(sess, "@ITEM", stItem), DataHelper.BuildParameter(sess, "@LOCATION", stLoc), DataHelper.BuildParameter(sess, "@QUANTITY_UM", stQtyUM), DataHelper.BuildOutputParameter(sess, "@Result", DbType.Int32, size) });
                num2 = size;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                num2 = 0;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return num2;
        }

        public static string LogException(Session sess, InventoryTransDS itDS)
        {
            return null;
        }

        public static string LogTransaction(Session sess, InventoryTransDS itDS)
        {
            return null;
        }

        public string TransferInventory(Session sess, InventoryDS fromInvDS, InventoryDS toInvDS, bool bWholeLPN)
        {
            DataTable table = null;
            Dictionary<string, KeyValuePair<string, object>> dcCond = null;
            int num = 0;
            LocationManager manager = null;
            ItemManager manager2 = null;
            List<InventoryChangeDS> arInvChgDS = null;
            InventoryChangeDS item = null;
            try
            {
                dcCond = new Dictionary<string, KeyValuePair<string, object>>();
                dcCond.Add("WAREHOUSE", new KeyValuePair<string, object>("=", fromInvDS.WAREHOUSE));
                dcCond.Add("LOCATION", new KeyValuePair<string, object>("=", fromInvDS.LOCATION));
                dcCond.Add("LPN", new KeyValuePair<string, object>("=", fromInvDS.LPN));
                dcCond.Add("ATTRIBUTE_NUM", new KeyValuePair<string, object>("=", fromInvDS.ATTRIBUTE_NUM));
                dcCond.Add("INVENTORY_STS", new KeyValuePair<string, object>("=", fromInvDS.INVENTORY_STS));
                manager = new LocationManager();
                manager2 = new ItemManager();
                arInvChgDS = new List<InventoryChangeDS>();
                if (!bWholeLPN)
                {
                    if (LocationManager.DoseLocTrackLPN(sess, fromInvDS.WAREHOUSE, fromInvDS.LOCATION) && StringManager.IsEmpty(fromInvDS.LPN))
                    {
                        return MessageManager.MSG_INV_003;
                    }
                    table = LocationInventoryDO.GetTableByColumns(sess, DictionaryManager.ToList(dcCond), null);
                    if (DataManager.IsEmpty(table))
                    {
                        return MessageManager.MSG_INV_001;
                    }
                    num = DataManager.GetInt(table.Rows[0], "ON_HAND_QTY") - DataManager.GetInt(table.Rows[0], "ALLOCATED_QTY");
                    if (num < fromInvDS.QUANTITY)
                    {
                        return MessageManager.MSG_INV_002;
                    }
                    if (LocationManager.DoseLocTrackLPN(sess, toInvDS.WAREHOUSE, toInvDS.LOCATION) && StringManager.IsEmpty(toInvDS.LPN))
                    {
                        return MessageManager.MSG_INV_003;
                    }
                    item = new InventoryChangeDS {
                        WAREHOUSE = fromInvDS.WAREHOUSE,
                        LOCATION = fromInvDS.LOCATION,
                        LPN = fromInvDS.LPN,
                        ITEM = fromInvDS.ITEM,
                        COMPANY = fromInvDS.COMPANY,
                        ATTRIBUTE_NUM = fromInvDS.ATTRIBUTE_NUM,
                        INVENTORY_STS = fromInvDS.INVENTORY_STS,
                        QUANTITY = fromInvDS.QUANTITY,
                        QUANTITY_UM = fromInvDS.QUANTITY_UM,
                        OP_ONHANDQTY = "-",
                        OP_INTRANQTY = "",
                        OP_ALLOCQTY = ""
                    };
                    arInvChgDS.Add(item);
                    item = new InventoryChangeDS {
                        WAREHOUSE = toInvDS.WAREHOUSE,
                        LOCATION = toInvDS.LOCATION,
                        LPN = toInvDS.LPN,
                        ITEM = toInvDS.ITEM,
                        COMPANY = toInvDS.COMPANY,
                        ATTRIBUTE_NUM = toInvDS.ATTRIBUTE_NUM,
                        INVENTORY_STS = toInvDS.INVENTORY_STS,
                        QUANTITY = toInvDS.QUANTITY,
                        QUANTITY_UM = toInvDS.QUANTITY_UM,
                        OP_ONHANDQTY = "+",
                        OP_INTRANQTY = "",
                        OP_ALLOCQTY = ""
                    };
                    arInvChgDS.Add(item);
                }
                this.AdjustInventory(sess, arInvChgDS);
                return null;
            }
            catch (Exception exception)
            {
                return (MessageManager.MSG_GEN_000 + exception.Message);
            }
        }

        public string ValidateAdjustment(Session sess, InventoryDS invDS, string op_OnHand, string op_InTransit, string op_Allocated)
        {
            string cmdText = "INV_VAdjustment";
            DataHelper helper = null;
            try
            {
                helper = new DataHelper(sess);
                return helper.GetString(CommandType.StoredProcedure, cmdText, new IDataParameter[] { 
                    DataHelper.BuildParameter(sess, "@WAREHOUSE", invDS.WAREHOUSE), DataHelper.BuildParameter(sess, "@LOCATION", invDS.LOCATION), DataHelper.BuildParameter(sess, "@ITEM", invDS.ITEM), DataHelper.BuildParameter(sess, "@COMPANY", invDS.COMPANY), DataHelper.BuildParameter(sess, "@LPN", invDS.LPN), DataHelper.BuildParameter(sess, "@ATTRIBUTE_NUM", invDS.ATTRIBUTE_NUM), DataHelper.BuildParameter(sess, "@ATTRIBUTE1", invDS.ATTRIBUTE1), DataHelper.BuildParameter(sess, "@ATTRIBUTE2", invDS.ATTRIBUTE2), DataHelper.BuildParameter(sess, "@ATTRIBUTE3", invDS.ATTRIBUTE3), DataHelper.BuildParameter(sess, "@ATTRIBUTE4", invDS.ATTRIBUTE4), DataHelper.BuildParameter(sess, "@ATTRIBUTE5", invDS.ATTRIBUTE5), DataHelper.BuildParameter(sess, "@ATTRIBUTE6", invDS.ATTRIBUTE6), DataHelper.BuildParameter(sess, "@ATTRIBUTE7", invDS.ATTRIBUTE7), DataHelper.BuildParameter(sess, "@ATTRIBUTE8", invDS.ATTRIBUTE8), DataHelper.BuildParameter(sess, "@QUANTITY", invDS.QUANTITY), DataHelper.BuildParameter(sess, "@OP_ONHAND", op_OnHand), 
                    DataHelper.BuildParameter(sess, "@OP_INTRANSIT", op_InTransit), DataHelper.BuildParameter(sess, "@OP_ALLOCATED", op_Allocated)
                 });
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return (MessageManager.MSG_GEN_000 + exception.Message);
            }
        }
    }
}

